﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：PurchaseOrderUnCheck.cs
    // 文件功能描述：采购单维护(未提交)
    // 创建标识：吕海乐2009-05-13
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Hospitals.Procurement
{
    public partial class PurchaseOrderUnCheck : BasePage
    {
        public PurchaseOrderUnCheck()
        {
            this.ModuleNo = Module.采购单管理.ToString();
        }

        IBLL_Purchase ip = Factory.BLL_Purchase();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StoreConditon();
                SetBind();
              
            }
        }

        /// <summary>
        /// 根据查询条件，绑定相应的信息
        /// </summary>
        private void SetBind()
        {
            HZ.DrugTrade.Model.Query.PurchaseOrder purchaseOrder = GetCondition();
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;
            IList<Model.View.PurchaseOrder> purchaseOrderList = ip.GetPurchaseOrderByWhere(purchaseOrder, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwPurchaseOrder.PageSize = AspNetPager1.PageSize;
            gvwPurchaseOrder.DataSource = purchaseOrderList;
            gvwPurchaseOrder.DataBind();
           
        }

        /// <summary>
        /// 将查询条件存储到viewstate中
        /// </summary>
        private void StoreConditon()
        {
            Model.Query.PurchaseOrder purchaseOrder = new HZ.DrugTrade.Model.Query.PurchaseOrder();
            purchaseOrder.OrderStatus = 0;
            purchaseOrder.OrderName = txtOrderName.Text.Trim();
            //purchaseOrder.PlanEndTimeFrom = new HZ.Common.ConvertHelper(txtEndTimeFrom.Text.Trim()).ToDateTime;
            //purchaseOrder.PlanEndTimeTo = new HZ.Common.ConvertHelper(txtEndTimeTo.Text.Trim()).ToDateTime;
            purchaseOrder.PlanTimeFrom = new HZ.Common.ConvertHelper(txtPlanTimeFrom.Text.Trim()).ToDateTime;
            purchaseOrder.PlanTimeTo = new HZ.Common.ConvertHelper(txtPlanTimeTo.Text.Trim()).ToDateTime;
            purchaseOrder.StartTimeFrom = new HZ.Common.ConvertHelper(txtStartTimeFrom.Text.Trim()).ToDateTime;
            purchaseOrder.StartTimeTo = new HZ.Common.ConvertHelper(txtStartTimeTo.Text.Trim()).ToDateTime;
            purchaseOrder.TotalAmountFrom = new HZ.Common.ConvertHelper(txtAccountFrom.Text.Trim()).ToDecimal;
            purchaseOrder.TotalAmountTo = new HZ.Common.ConvertHelper(txtAccountTo.Text.Trim()).ToDecimal;
            purchaseOrder.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            purchaseOrder.SortFields = "PlanTime DESC";

            ViewState["v_purchaseOrder"] = purchaseOrder;
        }

        /// <summary>
        /// 从viewstate中取出查询条件
        /// </summary>
        /// <returns></returns>
        private HZ.DrugTrade.Model.Query.PurchaseOrder GetCondition()
        {
            HZ.DrugTrade.Model.Query.PurchaseOrder purchaseOrder = (HZ.DrugTrade.Model.Query.PurchaseOrder)ViewState["v_purchaseOrder"];
            return purchaseOrder;
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            StoreConditon();
            SetBind();
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnButton_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            StoreConditon();
            SetBind();

        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwPurchaseOrder_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvwPurchaseOrder.EditIndex = e.NewEditIndex;
            SetBind();
        }

        /// <summary>
        /// 取消编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwPurchaseOrder_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvwPurchaseOrder.EditIndex = -1;
            SetBind();
        }

        /// <summary>
        /// 更新采购单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwPurchaseOrder_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Label lblOrderStatus = (Label)gvwPurchaseOrder.Rows[e.RowIndex].FindControl("lblOrderStatus");
            TextBox txtOrderName = (TextBox)gvwPurchaseOrder.Rows[e.RowIndex].FindControl("txtOrderName");
            TextBox txtStartTime = (TextBox)gvwPurchaseOrder.Rows[e.RowIndex].FindControl("txtStartTime");
            //TextBox txtEndTime = (TextBox)gvwPurchaseOrder.Rows[e.RowIndex].FindControl("txtPlanEndTime");
            //采购单名称为空
            if (string.IsNullOrEmpty(txtOrderName.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("采购单名称不能为空！", this);
                ((TextBox)gvwPurchaseOrder.Rows[e.RowIndex].FindControl("txtOrderName")).Focus();
                return;
            }
            //采购单时间为空
            if (string.IsNullOrEmpty(txtStartTime.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("采购单时间不能为空！", this);
                txtStartTime.Focus();
                return;
            }
            //采购开始时间不得早于现在的时间
            //if (Convert.ToDateTime(txtStartTime.Text.Trim()).Date < DateTime.Now.Date)
            //{
            //    HZ.Web.WebUtil.Alert("采购开始时间不得晚于当前时间", this);
            //    txtStartTime.Focus();
            //    return;
            //}
            //采购开始时间晚于结束时间
            //if (Convert.ToDateTime(txtStartTime.Text.Trim()) > Convert.ToDateTime(txtEndTime.Text.Trim()))
            //{
            //    HZ.Web.WebUtil.Alert("采购开始时间晚于结束时间！", this);
            //    txtStartTime.Focus();
            //    return;
            //}
            //更新采购单
            PurchaseOrder mPurchaseOrder = new PurchaseOrder();
            mPurchaseOrder.OrderID = gvwPurchaseOrder.DataKeys[e.RowIndex].Values[0].ToString();
            mPurchaseOrder.HospitalID = gvwPurchaseOrder.DataKeys[e.RowIndex].Values[1].ToString();
            mPurchaseOrder.OrderName = txtOrderName.Text.Trim();
            mPurchaseOrder.StartTime = new HZ.Common.ConvertHelper(txtStartTime.Text.Trim()).ToDateTime;
            //mPurchaseOrder.PlanEndTime = new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime;
            if (ip.UpdatePurchaseOrder(mPurchaseOrder) == 1)
            {
                gvwPurchaseOrder.EditIndex = -1;
                SetBind();
            }
            else
            {
                PageError("更新失败！", "");
            }
        }

        /// <summary>
        /// 简单查询与高级查询之间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtAccountFrom.Text = "";
                txtAccountTo.Text = "";
                //txtEndTimeFrom.Text = "";
                //txtEndTimeTo.Text = "";
                txtPlanTimeFrom.Text = "";
                txtPlanTimeTo.Text = "";
                txtStartTimeFrom.Text = "";
                txtStartTimeTo.Text = "";
            }
        }

        /// <summary>
        /// 绑定采购单状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwPurchaseOrder_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblOrderStatus = e.Row.FindControl("lblOrderStatus") as Label;
                TextBox txtOrderName = e.Row.FindControl("txtOrderName") as TextBox;
                TextBox txtStartTime = e.Row.FindControl("txtStartTime") as TextBox;
                if (lblOrderStatus.Text == "0")
                {
                    lblOrderStatus.Text = "未提交";
                }
                else if (lblOrderStatus.Text == "1")
                {
                    lblOrderStatus.Text = "已提交";
                }
            }
        }

        /// <summary>
        /// 删除采购单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwPurchaseOrder_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                PurchaseOrder dPurchaseOrder = new PurchaseOrder();
                dPurchaseOrder.OrderID = e.CommandArgument.ToString();
                if (ip.DeletePurchaseOrder(dPurchaseOrder) == 1)
                {
                    PageInfo("采购单删除成功！", this.Request.RawUrl);
                }
                else
                {
                    PageError("删除失败！", this.Request.RawUrl);
                }
            }
            else if (e.CommandName == "submit")
            {
                Model.Query.PurchaseOrderDetail qPurchaseOrderDetail = new HZ.DrugTrade.Model.Query.PurchaseOrderDetail();
                qPurchaseOrderDetail.OrderID = e.CommandArgument.ToString();
                qPurchaseOrderDetail.SortFields = " OrderID desc";
                int totalCount = 0;
                IList<Model.View.PurchaseOrderDetail> purchaseOrderDetailList = ip.GetPurchaseOrderDetailByWhere(qPurchaseOrderDetail, 1, 0, out totalCount);
                if (totalCount == 0)
                {
                    HZ.Web.WebUtil.Alert("采购单中没有任何产品，无法提交！", this);
                    return;
                }

                PurchaseOrder purchaseOrder = new PurchaseOrder();
                purchaseOrder.OrderID = e.CommandArgument.ToString();
                if (ip.UpdatePurchaseOrderDetail_Submit(purchaseOrder) == 1)
                {
                    PageInfo("采购单提交成功！", this.Request.RawUrl);
                }
                else
                {
                    PageError("提交失败！", "");
                }
            }
        }
    }
}
